//
//  UIImage+ceategory.swift
//  图片选择器
//
//  Created by x on 2017/7/21.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

extension UIImage {
    
    /// 根据传入的宽度生成一张图片,按照图片的宽高比来压缩以前的图片
    ///
    /// - Parameter width: 指定的宽度
    /// - Returns: <#return value description#>
    func imageWithWidth(width: CGFloat) -> UIImage {
        let height = width * size.height / size.width
        let currentSize = CGSize(width: width, height: height)
        
        //开始上下文 (相当于获取上下文)
        UIGraphicsBeginImageContext(currentSize)
        
        //按照原图画一张图片
        draw(in: CGRect.init(origin: CGPoint.zero, size: currentSize))
        
        //得到新画的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
        
        UIGraphicsEndImageContext()
        
        return newImage
    }
}
